/* 
 * Copyright (C) 2013, 2014 lex xiang
 *
 * file:	sys/shell.h
 * history:	2013 Juy 14 created by lex
 */
#ifndef __SHELL_H__
#define __SHELL_H__

#include "rbtree.h"
#include "tty.h"
#include "list.h"
#include "types.h"

#define NR_HIST_MAX	12
struct hist {
	struct list_head link;

	u8 line[TTY_BUF_MAX];
};

struct hist_pool {
	struct list_head	list;

	struct hist		*curr_hist;
	struct hist		*next_hist;
	struct hist		*prev_hist;
	struct hist		*latest;

	struct hist		entry[NR_HIST_MAX];
};

#define SH_BUF_MAX	512
#define NR_ARGC_MAX	32
#define SZ_ARGV_MAX	32
/*
 * shell info
 */
struct shell {
	char			*name;
	int			argc;
	u8			argv[NR_ARGC_MAX][SZ_ARGV_MAX];
	u8     			line[SH_BUF_MAX];

	struct tty		*tty;
	struct list_head	view_list;
	struct view_s		*curr_view;

	struct list_head	match_list;
	u32			nr_match;

	struct hist_pool	*hist;
};

#define LEN_VWNM_MAX	16

struct view_s {
	struct list_head	link;

	char			*name;
	char			*prompt;

	struct rb_tree		cmd_tr;
};

#define VIEW_ENTRY(name, prompt)	{{NULL, NULL}, #name, prompt, INIT_RB_TREE()}

#define shell_panic()	printf_d("painic of file: %s line: %d", __FILE__, __LINE__)

extern struct shell * shell_alloc();
extern void shell_free(struct shell *);
extern void shell_init(struct shell *sh, struct tty *tty);
extern void shell_do_parse(void *arg);
extern s32 rb_cmd_cmp(struct rb_node *, void *);
#endif	/* end of __SHELL_H__ */
